#!/bin/bash
# 方法二：手动指定环境变量安装 mysqlclient

# 首先查找 MySQL/MariaDB 库的位置
echo "正在查找 MySQL/MariaDB 库位置..."

# 常见路径
MYSQL_PATHS=(
    "/usr/include/mysql"
    "/usr/local/include/mysql"
    "/usr/include/mariadb"
    "/usr/local/include/mariadb"
    "/opt/mysql/include"
    "/opt/mariadb/include"
)

MYSQL_LIB_PATHS=(
    "/usr/lib64/mysql"
    "/usr/lib/mysql"
    "/usr/local/lib/mysql"
    "/usr/lib64/mariadb"
    "/usr/lib/mariadb"
    "/opt/mysql/lib"
    "/opt/mariadb/lib"
)

# 查找头文件目录
MYSQL_INCLUDE=""
for path in "${MYSQL_PATHS[@]}"; do
    if [ -d "$path" ]; then
        MYSQL_INCLUDE="$path"
        echo "找到头文件目录: $MYSQL_INCLUDE"
        break
    fi
done

# 查找库文件目录
MYSQL_LIB=""
for path in "${MYSQL_LIB_PATHS[@]}"; do
    if [ -d "$path" ] && [ -n "$(ls -A $path/libmysqlclient* 2>/dev/null)" ]; then
        MYSQL_LIB="$path"
        echo "找到库文件目录: $MYSQL_LIB"
        break
    fi
done

if [ -z "$MYSQL_INCLUDE" ] || [ -z "$MYSQL_LIB" ]; then
    echo "错误: 未找到 MySQL/MariaDB 库位置"
    echo "请先安装: sudo dnf install -y mariadb-devel mysql-devel"
    exit 1
fi

# 设置环境变量并安装
export MYSQLCLIENT_CFLAGS="-I$MYSQL_INCLUDE"
export MYSQLCLIENT_LDFLAGS="-L$MYSQL_LIB -lmysqlclient"

echo "环境变量设置:"
echo "MYSQLCLIENT_CFLAGS=$MYSQLCLIENT_CFLAGS"
echo "MYSQLCLIENT_LDFLAGS=$MYSQLCLIENT_LDFLAGS"
echo ""
echo "开始安装 mysqlclient..."

pip install mysqlclient

